import streamlit as st
from tools.verification import *
from tools.DBUtil import DBUtil
import pandas as pd

# ==================================================页面内容控制========================================================
st.title("系统配置")

# 读取数据库保存的配置信息
email_result = DBUtil.select_one("SELECT * FROM system_config LIMIT 1")
approval = email_result['approval']
email = email_result['email']
passwd = email_result['passwd']
server = email_result['server']
port = email_result['port']

st.subheader("一、虚拟机创建审批配置")
approval_type = st.radio("是否需要审批", ["否", "是"], index = approval,key="config_type")
approval_type = 0 if approval_type == "否" else 1
st.subheader("二、邮件服务配置")
col1,col2,col3,col4 = st.columns(4)
email_input = col1.text_input("邮箱地址", value=email, key="email_config_name")
passwd_input = col2.text_input("密码", value=passwd, key="email_config_passwd")
server_input = col3.text_input("服务器地址", value=server, key="email_config_server")
port_input = col4.text_input("端口", value=port, key="email_config_port")

# 保存配置信息
if st.button("保存"):
    if email_input and passwd_input and server_input and port_input:
        DBUtil.uid_db("UPDATE system_config SET approval=%s, email=%s, passwd=%s, server=%s, port=%s", (approval_type, email_input, passwd_input, server_input, port_input))
        st.success("保存成功！")
    else:
        st.error("请填写完整的配置信息！")
